package com.wing.bill.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.dao.UnusualAndCheckNewReportDao;
import com.wing.cabinet.model.response.StorageInventoryInoutVO;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UnusualAndCheckNewReportServiceImpl implements UnusualAndCheckNewReportService {
    @Autowired
    private UnusualAndCheckNewReportDao unusualAndCheckNewReportDao;

    @Override
    public JsonResult list(Integer abnormal,String param,  String enterpriseCode) {
        HashMap hm = new HashMap();
        String title = "";
        if(abnormal == null){
            title = "物料盘验统计(按天)";
        } else if(abnormal == 0){
            title = "物料异常统计(按天)";
        }
        hm.put("title",title);
        hm.put("data", unusualAndCheckNewReportDao.list(abnormal,param,enterpriseCode));
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult topCell(Integer abnormal,String startTime, String endTime, Integer click, String mainCabCode,String enterpriseCode) {
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            HashMap map1 = new HashMap();
            HashMap map2 = new HashMap();
            HashMap map3 = new HashMap();
            HashMap map4 = new HashMap();
            HashMap map5 = new HashMap();
            HashMap map6 = new HashMap();
            if (abnormal == null) {
                if (click == 0) {
                    map1 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, null, null, start, end, mainCabCode, enterpriseCode);
                    map1.put("title", "盘验物料总数量");
                    map1.put("sign", "1");

                    map2 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 1, null, start, end, mainCabCode, enterpriseCode);
                    map2.put("title", "盘验暂存数量");
                    map2.put("sign", "2");

                    map3 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 2, null, start, end, mainCabCode, enterpriseCode);
                    map3.put("title", "盘验报修数量");
                    map3.put("sign", "3");

                    map4 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 3, null, start, end, mainCabCode, enterpriseCode);
                    map4.put("title", "盘验报废数量");
                    map4.put("sign", "4");

                    map5 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, null, "规格不相符", start, end, mainCabCode, enterpriseCode);
                    map5.put("title", "盘验规格不相符数量");
                    map5.put("sign", "5");

                    map6 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 0, null, start, end, mainCabCode, enterpriseCode);
                    map6.put("title", "盘验缺失数量");
                    map6.put("sign", "6");

                    List list = new ArrayList();
                    list.add(map1);
                    list.add(map2);
                    list.add(map3);
                    list.add(map4);
                    list.add(map5);
                    list.add(map6);
                    return JsonResult.success("查询成功", list);
                } else if (click == 1) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal, null, null, start, end, mainCabCode, enterpriseCode));
                } else if (click == 2) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal,1,null,start, end, mainCabCode,enterpriseCode));
                } else if (click == 3) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal,2,null,start, end, mainCabCode,enterpriseCode));
                } else if (click == 4) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal,3,null,start, end, mainCabCode,enterpriseCode));
                } else if (click == 5) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal,null,"规格不相符",start, end, mainCabCode,enterpriseCode));
                } else if (click == 6) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal,0,null,start, end, mainCabCode,enterpriseCode));
                }
            } else {
                if (click == 0) {
                    map1 = unusualAndCheckNewReportDao.queryNumCheckPre(0, start, end, mainCabCode, enterpriseCode, 0);
                    map1.put("title", "报失数量（异常）");
                    map1.put("sign", "1");

                    map2 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, null, null, start, end, mainCabCode, enterpriseCode);
                    map2.put("title", "盘验刀具数量（异常）");
                    map2.put("sign", "2");

                    map3 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 1, null, start, end, mainCabCode, enterpriseCode);
                    map3.put("title", "暂存盘验异常数量");
                    map3.put("sign", "3");

                    map4 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 2, null, start, end, mainCabCode, enterpriseCode);
                    map4.put("title", "报修盘验异常数量");
                    map4.put("sign", "4");

                    map5 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 3, null, start, end, mainCabCode, enterpriseCode);
                    map5.put("title", "报废盘验异常数量");
                    map5.put("sign", "5");

                    map6 = unusualAndCheckNewReportDao.queryNumCheckAfter(abnormal, 0, null, start, end, mainCabCode, enterpriseCode);
                    map6.put("title", "盘验异常数量（缺失）");
                    map6.put("sign", "6");


                    List list = new ArrayList();
                    list.add(map1);
                    list.add(map2);
                    list.add(map3);
                    list.add(map4);
                    list.add(map5);
                    list.add(map6);
                    return JsonResult.success("查询成功", list);
                } else if (click == 1) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckPreClick(0, start, end, mainCabCode, enterpriseCode));
                } else if (click == 2) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal, null, null, start, end, mainCabCode, enterpriseCode));
                } else if (click == 3) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal, 1, null, start, end, mainCabCode, enterpriseCode));
                } else if (click == 4) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal, 2, null, start, end, mainCabCode, enterpriseCode));
                } else if (click == 5) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal, 3, null, start, end, mainCabCode, enterpriseCode));
                } else if (click == 6) {
                    return JsonResult.success("查询成功", unusualAndCheckNewReportDao.queryNumCheckAfterClick(abnormal, 0, null, start, end, mainCabCode, enterpriseCode));
                }

            }
        }
        return JsonResult.fail("查询失败");
    }

    @Override
    public JsonResult queryStatisticsByUnusual(String selectParam, String param, String startTime, String endTime, String mainCabCode, String enterpriseCode, String type, String tab, Map<String, Object> screenParam) {

        String title="物料异常";

        HashMap hm = new HashMap();
        if ("tab0".equals(tab)) {
            if ("sku".equals(type)) {
                hm.put("title", title+"排名(按物料)");
            } else if ("cab".equals(type)) {
                hm.put("title", title+"排名(按柜子)");
            } else if ("org".equals(type)) {
                hm.put("title", title+"排名(按组织)");
            } else if ("use".equals(type)) {
                hm.put("title", title+"排名(按使用情况)");
            } else if ("reason".equals(type)) {
                hm.put("title", "异常原因统计");
            }
        } else if ("tab1".equals(tab) && "spu".equals(selectParam)) {
            hm.put("title", title+"排名(按系列SPU)");
        } else if ("tab1".equals(tab) && "sku".equals(selectParam)) {
            hm.put("title", title+"排名(按SKU)");
        } else if ("tab1".equals(tab) && "type".equals(selectParam)) {
            hm.put("title", title+"排名(按类型)");
        } else if ("tab1".equals(tab) && "category".equals(selectParam)) {
            hm.put("title", title+"排名(按分类)");
        } else if ("tab1".equals(tab) && "brand".equals(selectParam)) {
            hm.put("title", title+"排名(按品牌)");
        } else if ("tab1".equals(tab) && "main".equals(selectParam)) {
            hm.put("title", title+"排名(按主柜)");
        } else if ("tab1".equals(tab) && "desk".equals(selectParam)) {
            hm.put("title", title+"排名(按副柜)");
        }  else if ("tab1".equals(tab) && "person".equals(selectParam)) {
            hm.put("title", title+"排名(按存入人)");
        } else if ("tab1".equals(tab) && "department".equals(selectParam)) {
            hm.put("title", title+"排名(按部门)");
        } else if ("tab1".equals(tab) && "costCenter".equals(selectParam)) {
            hm.put("title", title+"排名(按成本中心)");
        } else if ("tab1".equals(tab) && "workOrder".equals(selectParam)) {
            hm.put("title", title+"排名(按工单)");
        } else if ("tab1".equals(tab) && "workShop".equals(selectParam)) {
            hm.put("title", title+"排名(按车间)");
        } else if ("tab1".equals(tab) && "device".equals(selectParam)) {
            hm.put("title", title+"排名(按设备)");
        }


        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            if ("reason".equals(type)) {
                list.addAll(unusualAndCheckNewReportDao.queryReason(screenParam, start, end, mainCabCode, enterpriseCode));
            } else {
                list.addAll(unusualAndCheckNewReportDao.queryStatisticsByParam(selectParam, param, start, end, mainCabCode, enterpriseCode));
            }

            if ("department".equals(selectParam)||"costCenter".equals(selectParam)){
                hm.put("totalSum", unusualAndCheckNewReportDao.getTotal(start,end,param,mainCabCode,enterpriseCode));
            }


        }

        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult queryStatisticsByCheck(Integer businessType, String selectParam, String param, String startTime, String endTime, String mainCabCode, String enterpriseCode, String type, String tab) {
        String title="物料盘验";

        HashMap hm = new HashMap();
        List<HashMap> list = new ArrayList<>();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

            String classifyName = null;
            if ("tab1".equals(tab) && businessType == null && !"cab".equals(type)){
                classifyName = "规格不相符";
            }

            if ("tab0".equals(tab)) {
                if ("sku".equals(type)) {
                    hm.put("title", title+"排名(按物料)");
                } else if ("cab".equals(type)) {
                    hm.put("title", title+"排名(按柜子)");
                } else if ("org".equals(type)) {
                    hm.put("title", title+"排名(按组织)");
                } else if ("use".equals(type)) {
                    hm.put("title", title+"排名(按使用情况)");
                }

                if ("cab".equals(type)) {
                    list.addAll(unusualAndCheckNewReportDao.queryStatisticsByParam3(selectParam, param, start, end, mainCabCode, enterpriseCode));
                } else {
                    list.addAll(unusualAndCheckNewReportDao.queryStatisticsByParam2(selectParam, param, start, end, mainCabCode, enterpriseCode));
                }
            } else {
                if (businessType != null && businessType == 0) {
                    hm.put("title", title + "结果(按缺失)");
                }
                if (businessType != null && businessType == 1) {
                    hm.put("title", title + "结果(按暂存)");
                }
                if (businessType != null && businessType == 2) {
                    hm.put("title", title + "结果(按报修)");
                }
                if (businessType != null && businessType == 3) {
                    hm.put("title", title + "结果(按报废)");
                }
                if ("规格不相符".equals(classifyName)) {
                    hm.put("title", title + "结果(按规格不相符)");
                }
                if ("cab".equals(type)) {
                    if ("main".equals(selectParam)) {
                        hm.put("title", title+"排名(按主柜)");
                    } else if ( "desk".equals(selectParam)) {
                        hm.put("title", title+"排名(按副柜)");
                    }
                    list.addAll(unusualAndCheckNewReportDao.queryStatisticsByParam3(selectParam, param, start, end, mainCabCode, enterpriseCode));
                } else {
                    list.addAll(unusualAndCheckNewReportDao.queryStatisticsByTab(businessType, classifyName, selectParam, param, start, end, mainCabCode, enterpriseCode));
                }
            }

        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult queryCheckResultStatistics(Integer businessTypePre, Long creator, String startTime, String endTime, String mainCabCode, String enterpriseCode) {
        if (StringUtils.isBlank(startTime)&&StringUtils.isBlank(endTime)) {
            return JsonResult.fail("请传入日期");
        }
        LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
        LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");

        HashMap hm = new HashMap();
        List<HashMap> list = new ArrayList<>();

        if (businessTypePre == 1){
            hm.put("subTitle","暂存盘验数");
        } else if (businessTypePre == 2) {
            hm.put("subTitle","报修盘验数");
        } else if (businessTypePre == 3) {
            hm.put("subTitle","报废盘验数");
        }

        hm.put("totalSum", unusualAndCheckNewReportDao.queryNumCheckPre(businessTypePre,start,end,mainCabCode,enterpriseCode, null));

        Map<String,Object> params = new HashMap();
        params.put("businessTypePre",businessTypePre);
        params.put("start",start);
        params.put("end",end);
        params.put("mainCabCode",mainCabCode);
        params.put("enterpriseCode",enterpriseCode);
        params.put("creator",creator);

        list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(1,null,null,params));
        list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(0,0,null,params));
        list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(null,null,"规格不相符",params));
        if (businessTypePre == 1){
            list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(0,2,null,params));
            list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(0,3,null,params));
        } else if (businessTypePre == 2) {
            list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(0,1,null,params));
            list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(0,3,null,params));
        } else if (businessTypePre == 3) {
            list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(0,1,null,params));
            list.add(unusualAndCheckNewReportDao.queryCheckResultStatistics(0,2,null,params));
        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }


    @Override
    public Page data(String startTime, String endTime, String mainCabCode, String enterpriseCode, Page page) {
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {

            Map<String, Object> params = new HashMap<>();
            params.put("start",startTime);
            params.put("end",endTime);
            params.put("mainCabCode",mainCabCode);
            params.put("enterpriseCode",enterpriseCode);

            List<StorageInventoryInoutVO> list = unusualAndCheckNewReportDao.checkDataList(page,params);

            page.setRecords(list);
            return page;
        }
        return null;
    }
}
